package com.ebupt.migu.music.schedule.service.impl;

import com.ebupt.migu.music.api.entity.SendMessageEntity;
import com.ebupt.migu.music.api.service.ServiceErrorReportService;
import com.ebupt.migu.music.schedule.Enum.ExecuteStatusEnum;
import com.ebupt.migu.music.schedule.mapper.StrategyCommonTaskMapper;
import com.ebupt.migu.music.schedule.service.StrategyCommonTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2019/10/27
 */
@Slf4j
@Service
public class StrategyCommonTaskServiceImpl implements StrategyCommonTaskService {

    @Autowired
    private StrategyCommonTaskMapper strategyCommonTaskMapper;

    @Autowired
    private ServiceErrorReportService serviceErrorReportService;

    /**
     * 每日零点扫描策略表,修改策略执行状态：
     * 仅针对BANNER和音乐之声策略
     * 1.将今日需要执行的策略状态修改为进行中
     * 2.将今日需要执行的间隔策略标记为今日有效
     * 3.将今日不需要执行的间隔策略标记为今日无效
     * 4.将今日执行过并且今日为策略截至时间的策略状态修改为已结束
     */
    @Override
    public void updateStrategyExecuteStatus() {
        log.info("修改策略运行状态定时任务启动");
        List<HashMap<String, Object>> starts = strategyCommonTaskMapper.toBeStart();
        List<HashMap<String, Object>> ends = strategyCommonTaskMapper.toBeEnd();

        if ((null == starts || starts.isEmpty()) && (null == ends || ends.isEmpty())) {
            log.info("当前没有策略需要修改状态，本次任务结束");
            return;
        }

        // 将未开始的策略修改成进行中
        if (null != starts || !starts.isEmpty()) {
            for (Map<String, Object> start : starts) {
               // String sid = start.get("strategyId").toString();
                start.put("executeStatus", ExecuteStatusEnum.CONDUCT_ING.getCode());
                strategyCommonTaskMapper.updateExecuteStatusByStrategyId(start);
            }
        }

        // 将进行中或已中止的策略状态修改成已结束
        if (null != ends || !ends.isEmpty()) {
            for (Map<String, Object> end : ends) {
              //  String sid = end.get("strategyId").toString();
                end.put("executeStatus", ExecuteStatusEnum.END.getCode());
                strategyCommonTaskMapper.updateExecuteStatusByStrategyId(end);
            }
        }

        log.info("任务完成，本次共对{}条策略进行状态修改", starts.size() + ends.size());
    }

    /**
     * 针对已暂停的策略
     * 策略类型包含定时、循环、间隔
     * 暂停状态的过程中，当前时间超过策略执行时间【定时】或者截止时间【循环、间隔】，将策略状态修改为结束
     */
    @Override
    public void tac_overdue() {
        strategyCommonTaskMapper.tac_overdue_start_time();
        strategyCommonTaskMapper.tac_overdue_end_time();
    }

    /**
     * 发送短信：
     * 1、 短信通知运维- xx人群等待更新超时
     * 2、 短信通知运维- xx策略等待发送超时
     */
    @Override
    public void send_message() {

        List<String> gids = strategyCommonTaskMapper.overtime_groups();
        List<String> sids = strategyCommonTaskMapper.overtime_tacs();
        if (sids != null && sids.size() != 0) {
            for (String sid : sids) {
                SendMessageEntity sendMessageEntity = new SendMessageEntity();
                sendMessageEntity.setAddressee("2");
                sendMessageEntity.setTac_id(sid);
                sendMessageEntity.setError_code("2");
                sendMessageEntity.setTac_id(sid);
                serviceErrorReportService.send_message(sendMessageEntity);
                Map<String, String> map = new HashMap<>();
                map.put("sid", sid);
                strategyCommonTaskMapper.error_report_history(map);
            }
        }
        if (gids != null && gids.size() != 0) {
            for (String gid : gids) {
                SendMessageEntity sendMessageEntity = new SendMessageEntity();
                sendMessageEntity.setAddressee("2");
                sendMessageEntity.setTac_id(gid);
                sendMessageEntity.setError_code("5");
                sendMessageEntity.setGroup_id(gid);
                serviceErrorReportService.send_message(sendMessageEntity);
                Map<String, String> map = new HashMap<>();
                map.put("gid", gid);
                strategyCommonTaskMapper.error_report_history(map);
            }
        }
    }

}
